import React,{Component} from 'react';
import CookieUtil from './utils/CookieUtil';
import $ from 'jquery';
import { hashHistory } from 'react-router';
import HandleSearch from './utils/HandleSearch';

class Guide extends Component {
    state = {

    }

    componentWillMount() {
        /*const openid = CookieUtil.getCookie('openid');
         if(!openid){
             const appid = 'wx06c0a14577c208b3';
             const appsecret = 'c70fb7407b2f010933715a92837446e5';
             const scope = 'snsapi_userinfo';
             const state = 'thisisstate';
             const redirect_uri = 'http://vhjmlw.free.ngrok.cc/handleCode';
             // const redirect_uri = '/App';
             let url = `https://open.weixin.qq.com/connect/oauth2/authorize`;
             url += `?appid=${appid}`;
             url += `&redirect_uri=${redirect_uri}`;
             url += `&response_type=code`;
             url += `&scope=${scope}`;
             url += `&state=${state}#wechat_redirect`;
             /!*$.ajax({
             url,
             type: 'GET',
             success: (response)=>{
             console.log(response);
             },
             error: (error)=>{
             throw error;
             },
             });*!/
             // window.open(url,'_parent');
             window.location.href = url;
         } else {
             hashHistory.push('/App');
         }*/

    //    redirect_uri指向前端的页面
        const openid = CookieUtil.getCookie('openid');
        if(!openid){
            const params = HandleSearch.parseQueryString(window.location.href);
            const { code, state } = params;
            alert(code);
            alert(state);
            $.ajax({
                url: `/handleCode?code=${code}&state=${state}`,
                type: 'GET',
                success: (response)=>{
                    console.log(response);
                    hashHistory.push('/App');
                },
                error: (err)=>{
                    if(err){
                        throw err;
                    }
                }
            });
        } else {
            hashHistory.push('/App');
        }
    }

    render(){
        return (
            <div>hello world</div>
        );
    }

}

export default Guide;